package com.buka.goods.controller;


import com.buka.entity.PageResult;
import com.buka.entity.Result;
import com.buka.entity.StatusCode;
import com.buka.goods.service.SpecService;
import com.buka.server.pojo.Brand;
import com.buka.server.pojo.GoodsSpec;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    SpecService specService;

    @RequestMapping("/findAll")
    public Result<List<GoodsSpec>> findAll(){
        List<GoodsSpec> list=specService.findAll();
        return new Result<>(true, StatusCode.OK,"查询成功",list);
    }

    @RequestMapping("/findById/{id}")
    public Result<List<GoodsSpec>> findById(@PathVariable Long id){
        List<GoodsSpec> list=specService.findById(id);
        return new Result<>(true, StatusCode.OK,"查询成功",list);
    }

    @PostMapping("/add")
    public Result addNewSpec(@RequestBody GoodsSpec goodsSpec){
        int a=specService.addGoodsSpec(goodsSpec);
        if(a!=1){
            return new Result(false,StatusCode.ERROR,"增加失败");
        }else return new Result<>(true,StatusCode.OK,"增加成功");
    }

    @PostMapping("/update")
    public Result UpdateSpec(@RequestBody GoodsSpec goodsSpec){
        int a=specService.updateGoodsSpec(goodsSpec);
        if(a!=1){
            return new Result(false,StatusCode.ERROR,"更新失败");
        }else return new Result<>(true,StatusCode.OK,"更新成功");
    }

    @RequestMapping("/delete/{id}")
    public Result DeteleSpec(@PathVariable Long id){
        int a=specService.deleteFalseGoodsSpec(id);
        if(a!=1){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }else return new Result<>(true,StatusCode.OK,"删除成功");
    }

    @RequestMapping("/search/{s}")
    public Result<List<GoodsSpec>> Search(@PathVariable("s")String specName){
        List<GoodsSpec> list=specService.FuzzyQuery(specName);
        return new Result<>(true,StatusCode.OK,"查询成功",list);

    }

    @RequestMapping("/findAllPage")
    public Result<List<GoodsSpec>> findAllPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<GoodsSpec> list=specService.findPage(page,size);
        PageResult pageResult=new PageResult(list.getTotal(),list.getResult());
        return new Result<>(true, StatusCode.OK,"查询成功",pageResult);
    }
    @RequestMapping("/findByName")
    public Result<List<GoodsSpec>> findAllPage(@RequestParam String specName,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<GoodsSpec> list=specService.findPage(specName,page,size);
        PageResult pageResult=new PageResult(list.getTotal(),list.getResult());
        return new Result<>(true, StatusCode.OK,"查询成功",pageResult);
    }
}
